﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace Passwords.Biz
{

    public interface IIdAccess
    {
        void SetId(int id);
    }

    public abstract class Entity : INotifyPropertyChanged, IIdAccess
    {
        private string name;

        public Entity()
            : base()
        {
            EntityState = EntityState.New;
        }

        public Entity(int id, string name)
            : base()
        {
            EntityState = EntityState.Unchanged;
            this.Id = id;
            this.name = name;
        }

        public Context Context { get { return Context.Current; } }

        public virtual string Name
        {
            get { return name ?? string.Empty; }
            set
            {
                name = value;
                OnPropertyChanged("Name");
            }
        }

        public int Id { get; private set; }

        public bool IsNew { get { return EntityState == EntityState.New; } }
        public bool IsDeleted { get { return EntityState == EntityState.Deleted; } }
        public bool IsModified { get { return EntityState == EntityState.Modified; } }
        public bool IsNewOrModified { get { return EntityState == EntityState.New || EntityState == EntityState.Modified; } }

        public EntityState EntityState { get; protected set; }

        #region INotifyPropertyChanged Members

        public virtual void OnPropertyChanged(string propertyName)
        {
            if (EntityState == EntityState.Unchanged) EntityState = EntityState.Modified;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IIdAccess Members

        void IIdAccess.SetId(int id)
        {
            this.Id = id;
        }

        #endregion
    }
}
